﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows.Forms;

using Elegant.Ui;

namespace iPdf
{
    class TataComboBox: Elegant.Ui.ComboBox
    {
        #region Fields
        protected Elegant.Ui.DropDown dropDownMenu;
        protected const string comboBoxPopupItemTypeName = "ComboBoxDropDownListItem";
        protected bool mouseHoverEventHandlerInstalled;
        protected object hoveredItem;
        
        public event EventHandler HoveredItemChanged;
        #endregion

        protected Elegant.Ui.DropDown DropDownMenu
        {
            get
            {
                if (dropDownMenu == null)
                {
                    foreach (System.Windows.Forms.Control control in this.Controls)
                    {
                        if (control is Elegant.Ui.DropDown)
                        {
                            dropDownMenu = (Elegant.Ui.DropDown)control;
                            break;
                        }
                    }

                    dropDownMenu.IsPopupOpenChanged += new EventHandler(DropDownMenu_IsPopupOpenChanged);
                }

                return dropDownMenu;
            }
        }

        protected Elegant.Ui.Popup PopupMenu
        {
            get
            {
                return DropDownMenu == null ? null : (Elegant.Ui.Popup)DropDownMenu.Popup;
            }
        }

        protected ControlCollection PopupChildControls
        {
            get
            {
                if (PopupMenu == null)
                {
                    return null;
                }

                System.Windows.Forms.Control child = PopupMenu.Child;
                if (child == null)
                {
                    return null;
                }

                return child.Controls;
            }
        }

        /// <summary>
        /// This is the result of hacking into the core of the Elegant.Ui by using 
        /// the debugging facility of Visual C# and the .Net Reflector
        /// </summary>
        protected Collection<Elegant.Ui.Control> PopupItems
        {
            get
            {
                var popupItems = new Collection<Elegant.Ui.Control>();

                foreach (Elegant.Ui.Control control in PopupChildControls)
                {
                    Type controlType = control.GetType();
                    if (controlType.Name == comboBoxPopupItemTypeName)
                    {
                        popupItems.Add(control);
                    }
                }

                return popupItems;
            }
        }

        public object HoveredItem
        {
            get
            {
                return hoveredItem;
            }
        }

        protected override void OnDropDown()
        {
            base.OnDropDown();

            if (!mouseHoverEventHandlerInstalled)
            {
                mouseHoverEventHandlerInstalled = true;
                InstallHoverHandlersForPopupItems();
            }
        }

        private void DropDownMenu_IsPopupOpenChanged(object sender, EventArgs e)
        {
            if (this.dropDownMenu.IsPopupOpen == false)
            {
                this.hoveredItem = null;
                if (this.HoveredItemChanged != null)
                {
                    this.HoveredItemChanged(this, new EventArgs());
                }
            }
        }

        private void InstallHoverHandlersForPopupItems()
        {
            int index = 0;
            foreach (object item in Items)
            {
                Elegant.Ui.Control control = PopupItems[index++];
                control.Tag = item;
                control.MouseEnter += new EventHandler(PopupItem_MouseHover);
            }
        }

        private void PopupItem_MouseHover(object sender, EventArgs e)
        {
            Elegant.Ui.Control control = sender as Elegant.Ui.Control;
            hoveredItem = control.Tag;
            if (this.HoveredItemChanged != null)
            {
                this.HoveredItemChanged(this, new EventArgs());
            }
        }

    }
}
